<%@ taglib prefix="spring-tags" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="spring-form" uri="http://www.springframework.org/tags/form"%>
<%@ page
    import="org.jiscinvolve.astrodabis.barberry.control.Paths"
    import="org.jiscinvolve.astrodabis.barberry.control.ObjectData"
    import="org.jiscinvolve.astrodabis.barberry.control.RequestData"
    import="org.jiscinvolve.astrodabis.barberry.control.jobs.CreateJobBean"

    import="org.jiscinvolve.astrodabis.barberry.common.core.Job"
    import="org.jiscinvolve.astrodabis.barberry.common.core.Job.TagNameSrc"
    import="org.jiscinvolve.astrodabis.barberry.common.core.Job.TagDataSrc"
    import="org.jiscinvolve.astrodabis.barberry.common.core.Job.SourceType"

    import="org.jiscinvolve.astrodabis.barberry.common.core.Tag"
    import="org.jiscinvolve.astrodabis.barberry.common.core.Womble"
    import="org.jiscinvolve.astrodabis.barberry.common.core.Account"
    import="org.jiscinvolve.astrodabis.barberry.common.core.Identifier"
    import="org.jiscinvolve.astrodabis.barberry.common.core.exception.*"

    import="org.jiscinvolve.astrodabis.barberry.common.data.TapTable"
    import="org.jiscinvolve.astrodabis.barberry.common.data.TapColumn"
    import="org.jiscinvolve.astrodabis.barberry.common.data.TapService"

    session="true"
%><%
RequestData data = (RequestData) request.getAttribute(
    RequestData.MODEL_ATTRIB
    ) ;
Womble  womble = data.womble();
Account actor  = data.actor();

CreateJobBean bean =  (CreateJobBean) request.getAttribute(
    "bean"
    ) ;
%>
<html>
    <head>
	    <title>AstroDAbis data import</title>
        <link href='<%= data.file("css/page.css") %>' rel='stylesheet' type='text/css'/>
        <script type='text/javascript' src='<%= data.file("js/jquery-1.6.4.js") %>'></script>
        <script type='text/javascript'>
            /*
             * http://stackoverflow.com/questions/6248774/how-to-populate-select-box-using-jquery-in-spring-forms
             */
            $(document).ready(
                function()
                    {
                    //
                    // On change of TapService one
                    $('select#sidOne').change(
                        function()
                            {
                            $('select#tidOne').html('<option value="-">TAP table</option>');
                            $('select#cidOne').html('<option value="-">TAP column</option>');
                            $.get(
                                "<%= data.link(Paths.SERVICE_PATH) %>" + "/" + $(this).val() + "/tables/json",
                                function(tables)
                                    {
                                    var opts = '<option value="-">TAP table</option>';
                                    $.each(
                                        tables,
                                        function()
                                            { 
                                            opts += '<option value="' + this.ident + '">' + this.name + '</option>';
                                            }
                                        );
                                    $('select#tidOne').html(opts);
                                    }
                                ); 
                            }
                        );

                    //
                    // On change of TapTable one
                    $('select#tidOne').change(
                        function()
                            {
                            $('select#cidOne').html('<option value="-">TAP column</option>');
                            $.get(
                                "<%= data.link(Paths.TABLE_PATH) %>" + "/" + $(this).val() + "/columns/json",
                                function(columns)
                                    {
                                    var opts = '<option value="-">TAP column</option>';
                                    $.each(
                                        columns,
                                        function()
                                            { 
                                            opts += '<option value="' + this.ident + '">' + this.name + '</option>';
                                            }
                                        );
                                    $('select#cidOne').html(opts);
                                    }
                                ); 
                            }
                        );
                    //
                    // On change of 'associated' check box
                    $('input#joinedCheck').change(
                        function()
                            {
                            if ($(this).is(":checked"))
                                {
                                $('span#joinedNone').hide();
                                $('div#joinedSelect').slideDown();
                                }
                            else {
                                $('span#joinedNone').show();
                                $('div#joinedSelect').slideUp();
                                }
                            }
                        );
                    //
                    // On change of TapService two
                    $('select#sidTwo').change(
                        function()
                            {
                            $('select#tidTwo').html('<option value="-">TAP table</option>');
                            $('select#cidTwo').html('<option value="-">TAP column</option>');
                            $.get(
                                "<%= data.link(Paths.SERVICE_PATH) %>" + "/" + $(this).val() + "/tables/json",
                                function(tables)
                                    {
                                    var opts = '<option value="-">TAP table</option>';
                                    $.each(
                                        tables,
                                        function()
                                            { 
                                            opts += '<option value="' + this.ident + '">' + this.name + '</option>';
                                            }
                                        );
                                    $('select#tidTwo').html(opts);
                                    }
                                ); 
                            }
                        );
                    //
                    // On change of TapTable two
                    $('select#tidTwo').change(
                        function()
                            {
                            $('select#cidTwo').html('<option value="-">TAP column</option>');
                            $.get(
                                "<%= data.link(Paths.TABLE_PATH) %>" + "/" + $(this).val() + "/columns/json",
                                function(columns)
                                    {
                                    var opts = '<option value="-">TAP column</option>';
                                    $.each(
                                        columns,
                                        function()
                                            { 
                                            opts += '<option value="' + this.ident + '">' + this.name + '</option>';
                                            }
                                        );
                                    $('select#cidTwo').html(opts);
                                    }
                                ); 
                            }
                        );
                    //
                    // On change of TagNameSrc
                    $('select#tagNameSrc').change(
                        function()
                            {
                            if ($(this).val() == 'INPAGE')
                                {
                                $('span#tagNameSpan').show();
                                $('span#tagNameText').html('');
                                }
                            else if ($(this).val() == 'INDATA')
                                {
                                $('span#tagNameSpan').hide();
                                $('span#tagNameText').html('generated from data [tagname]');
                                }
                            else {
                                $('span#tagNameSpan').hide();
                                $('span#tagNameText').html('');
                                }
                            }
                        );
                    //
                    // On change of TagDataSrc
                    $('select#tagDataSrc').change(
                        function()
                            {
                            if ($(this).val() == 'INPAGE')
                                {
                                $('span#tagDataSpan').show();
                                $('span#tagDataText').html('');
                                }
                            else if ($(this).val() == 'INDATA')
                                {
                                $('span#tagDataSpan').hide();
                                $('span#tagDataText').html('generated from data [tagvalue]');
                                }
                            else {
                                $('span#tagDataSpan').hide();
                                $('span#tagDataText').html('');
                                }
                            }
                        );
                    //
                    // On change of SourceType
                    $('select#sourceType').change(
                        function()
                            {
                            if ($(this).val() == 'UPLOAD')
                                {
                                $('span#sourceUrl').hide();
                                $('span#sourceFile').show();
                                $('div#sourceQuery').hide();
                                }
                            else if ($(this).val() == 'IMPORT')
                                {
                                $('span#sourceUrl').show();
                                $('span#sourceFile').hide();
                                $('div#sourceQuery').hide();
                                }
                            else if ($(this).val() == 'QUERY')
                                {
                                $('span#sourceUrl').hide();
                                $('span#sourceFile').hide();
                                $('div#sourceQuery').show();
                                }
                            else {
                                $('span#sourceUrl').hide();
                                $('span#sourceFile').hide();
                                $('div#sourceQuery').hide();
                                }
                            }
                        );
                    }
                );
        </script>
    </head>
    <body>
        <%@ include file="/jsp/header.jsp" %>
        <div class='body'>
            <div class='left'>
                <%@ include file="/jsp/menu.jsp" %>
            </div>
            <div class='right'>
            </div>
            <div class='content'>
		        <h2>New data import</h2>
      			<spring-form:errors path="bean"/>
		        <spring-form:form method="post" modelAttribute="bean" enctype="multipart/form-data">
		          	<fieldset>
		          		<legend>Name</legend>
	              		<spring-form:input  path="name"/>
              			<spring-form:errors path="name"/>
		          	</fieldset>
		          	<fieldset>
		          		<legend>Description</legend>
	              		<spring-form:textarea rows='3' cols='50' path="text"/>
	          			<spring-form:errors path="text"/>
		          	</fieldset>

		          	<fieldset>
		          		<legend>Target data</legend>
                        <div>
                            Annotations about
                            <!-- [<%= bean.getSidOne() %>][<%= bean.getTidOne() %>][<%= bean.getCidOne() %>] -->
                            <div>
                      			<spring-form:errors path="sidOne" class="warning"/>
                      			<spring-form:errors path="tidOne" class="warning"/>
                      			<spring-form:errors path="cidOne" class="warning"/>
                          		<spring-form:select path="sidOne" id="sidOne">
                                    <spring-form:option value="-" label="TAP service"/>
                                    <%
                                    for (TapService service : womble.services().select())
                                        {
                                        %>
                                        <spring-form:option value="<%= service.ident() %>" label="<%= service.name() %>"/>
                                        <%
                                        }
                                    %>
                          		</spring-form:select>
                          		<spring-form:select path="tidOne" id="tidOne">
                                    <spring-form:option value="-" label="TAP table"/>
                                    <%
                                    if (bean.srvOne() != null)
                                        {
                                        for(TapTable table : bean.srvOne().tables().select())
                                            {
                                            %>
                                            <spring-form:option value="<%= table.ident() %>" label="<%= table.name() %>"/>
                                            <%
                                            }
                                        }
                                    %>
                          		</spring-form:select>
                          		<spring-form:select path="cidOne" id="cidOne">
                                    <spring-form:option value="-" label="TAP column"/>
                                    <%
                                    if (bean.tabOne() != null)
                                        {
                                        for(TapColumn column : bean.tabOne().columns().select())
                                            {
                                            %>
                                            <spring-form:option value="<%= column.ident() %>" label="<%= column.name() %>"/>
                                            <%
                                            }
                                        }
                                    %>
                          		</spring-form:select>
                            </div>
                        </div>

                        <div>
                            associated with
                            <!-- [<%= bean.getSidTwo() %>][<%= bean.getTidTwo() %>][<%= bean.getCidTwo() %>] -->
                            <spring-form:checkbox path="joined" id="joinedCheck"/>
                            <span id="joinedNone"   style="<%= (bean.getJoined()) ? "display:none" : "" %>">(none)</span>
                            <div  id="joinedSelect" style="<%= (bean.getJoined()) ? "" : "display:none" %>">
                      			<spring-form:errors path="sidTwo" class="warning"/>
                      			<spring-form:errors path="tidTwo" class="warning"/>
                      			<spring-form:errors path="cidTwo" class="warning"/>
                          		<spring-form:select path="sidTwo" id="sidTwo">
                                    <spring-form:option value="-" label="TAP service"/>
                                    <%
                                    for (TapService service : womble.services().select())
                                        {
                                        %>
                                        <spring-form:option value="<%= service.ident() %>" label="<%= service.name() %>"/>
                                        <%
                                        }
                                    %>
                          		</spring-form:select>
                          		<spring-form:select path="tidTwo" id="tidTwo">
                                    <spring-form:option value="-" label="TAP table"/>
                                    <%
                                    if (bean.srvTwo() != null)
                                        {
                                        for(TapTable table : bean.srvTwo().tables().select())
                                            {
                                            %>
                                            <spring-form:option value="<%= table.ident() %>" label="<%= table.name() %>"/>
                                            <%
                                            }
                                        }
                                    %>
                          		</spring-form:select>
                          		<spring-form:select path="cidTwo" id="cidTwo">
                                    <spring-form:option value="-" label="TAP column"/>
                                    <%
                                    if (bean.tabTwo() != null)
                                        {
                                        for(TapColumn column : bean.tabTwo().columns().select())
                                            {
                                            %>
                                            <spring-form:option value="<%= column.ident() %>" label="<%= column.name() %>"/>
                                            <%
                                            }
                                        }
                                    %>
                          		</spring-form:select>
                            </div>
                        </div>
		          	</fieldset>

		          	<fieldset>
		          		<legend>Tagged with</legend>
                        <div>
              			    <spring-form:errors path="tagName" class="warning"/>
                            name 
                      		<spring-form:select path="tagNameSrc" id="tagNameSrc">
                                <spring-form:option value="INPAGE" label="in form"/>
                                <spring-form:option value="INDATA" label="in data"/>
                      		</spring-form:select>
                            <span id="tagNameText">
                                <%= (bean.getTagNameSrc() == TagNameSrc.INDATA) ? "generated from data [tagname]" : "" %>
                            </span>
                            <span id="tagNameSpan" style="<%= (bean.getTagNameSrc() == TagNameSrc.INPAGE) ? "display:inline" : "display:none"%>">
                          		<spring-form:input  path="tagName"/>
                            </span>
                        </div>
                        <div>
                  			<spring-form:errors path="tagData" class="warning"/>
                            value 
                      		<spring-form:select path="tagDataSrc" id="tagDataSrc">
                                <spring-form:option value="INPAGE" label="in form"/>
                                <spring-form:option value="INDATA" label="in data"/>
                                <spring-form:option value="NONE"   label="none"/>
                      		</spring-form:select>
                            <span id="tagDataText">
                                <%= (bean.getTagDataSrc() == TagDataSrc.INDATA) ? "generated from data [tagvalue]" : "" %>
                            </span>
                            <span id="tagDataSpan" style="<%= (bean.getTagDataSrc() == TagDataSrc.INPAGE) ? "display:inline" : "display:none"%>">
                          		<spring-form:input  path="tagData"/>
                            </span>
                        </div>
		          	</fieldset>

		          	<fieldset>
		          		<legend>Loaded from</legend>
                        <div>
                  			<spring-form:errors path="sourceUrl"  class="warning"/>
                  			<spring-form:errors path="sourceFile"  class="warning"/>

                      		<spring-form:select path="sourceType" id="sourceType">
                                <spring-form:option value="UPLOAD" label="file upload"/>
                                <!--spring-form:option value="IMPORT" label="import from URL"/-->
                                <!--spring-form:option value="QUERY"  label="TAP query"/-->
                      		</spring-form:select>
                            <span id="sourceUrl" style="<%= (bean.getSourceType() == SourceType.IMPORT) ? "display:inline" : "display:none"%>">
                          		<spring-form:input path="sourceUrl" size="40"/>
                            </span>
                            <span id="sourceFile" style="<%= (bean.getSourceType() == SourceType.UPLOAD) ? "display:inline" : "display:none"%>">
                                <spring-form:input path="sourceFile" type="file"/>
                            </span>
                            <div id="sourceQuery" style="<%= (bean.getSourceType() == SourceType.QUERY) ? "display:inline" : "display:none"%>">
                      			<spring-form:errors path="sourceQuerySid" class="warning"/>
                      			<spring-form:errors path="sourceQueryText" class="warning"/>
                                <div>
                              		<spring-form:select path="sourceQuerySid" id="sourceQuerySid">
                                        <spring-form:option value="-" label="TAP service"/>
                                        <%
                                        for (TapService service : womble.services().select())
                                            {
                                            %>
                                            <spring-form:option value="<%= service.ident() %>" label="<%= service.ivorn().toString() %>"/>
                                            <%
                                            }
                                        %>
                              		</spring-form:select>
                                </div>
                                <div>
	                          		<spring-form:textarea rows='3' cols='50' path="sourceQueryText"/>
                                </div>
                            </div>
                        </div>
		          	</fieldset>

			        <div>
			            <button type="submit">Create</button>
		            </div>
		        </spring-form:form>
            </div>
        </div>
        <%@ include file="/jsp/footer.jsp" %>
    </body>

